﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ProjectN.Core.Element.Def;
using ProjectN.Core.Instance.Data;
using System.Security.Cryptography;
using System.IO;

namespace ProjectN.Core.Fore.Editor
{
    public partial class ImageInfo_EditorControl : UserControl
    {
        public ImageInfo_EditorControl()
        {
            InitializeComponent();
            IsChanged = false;
        }

        public ImageInfo? Info { get; set; }

        private static readonly OpenFileDialog openFileDialog = new()
        {
            Filter = "图片文件|*.bmp;*.jpg;*.jpeg;*.png;*.tiff|BMP 文件|*.bmp|JPEG 文件|*.jpg;*.jpeg|PNG 文件|*.png|TIFF 文件|*.tiff|所有文件|*.*"
        };

        private string imageFn = string.Empty;
        private string SHAValue
        {
            get => ShaDisplayLabel.Text;
            set => ShaDisplayLabel.Text = value;
        }

        private bool _isChanged;
        private bool IsChanged
        {
            get => _isChanged;
            set
            {
                _isChanged = value;
                AcceptButton.Enabled = _isChanged;
                ResetButton.Enabled =  _isChanged;
            }
        }
        private void ReadFromInfo()
        {
            if (Info == null) return;
            var handler = Info.Data.ImageCollection.GetHandler(Info.Identity)
                            ?? throw new InvalidOperationException("图片不存在");
            SHAValue = handler.Hash;
            var img = PictureBox.Image = handler.Image;
            SizeLabel.Text = $"{img.Width} x {img.Height}";
        }

        private void SaveToInfo()
        {
            if (Info == null) return;
            Info.Data.ImageCollection.StoreFile(imageFn);
            Info.Identity = SHAValue;
            imageFn = string.Empty;
        }
        private void OpenButton_Click(object sender, EventArgs e)
        {
            var result = openFileDialog.ShowDialog(Parent as Form);
            if (result == DialogResult.OK)
            {
                try
                {
                    imageFn = openFileDialog.FileName;
                    if(Info != null)
                        this.SHAValue = Info.Data.ImageCollection.CalculateIdentity(imageFn);
                    var img = PictureBox.Image = Image.FromFile(imageFn);
                    SizeLabel.Text = $"{img.Width} x {img.Height}";
                    IsChanged = true;
                } catch (Exception ex)
                {
                    ErrorInfoBox.Create(ex, "打开图片失败", "未能成功打开图片，这可能是由于格式不受支持或文件已损坏。");
                }
            }
        }

        private void ResetButton_Click(object sender, EventArgs e)
        {
            var result = MessageBox.Show("确认重置修改吗？", "重置确认", MessageBoxButtons.OKCancel);
            if(result == DialogResult.OK)
            {
                ReadFromInfo();
                IsChanged = false;
            }
        }

        private void AcceptButton_Click(object sender, EventArgs e)
        {
            SaveToInfo();
            IsChanged = false;
        }

        private void ImageInfo_EditorControl_Load(object sender, EventArgs e)
        {
            ReadFromInfo();
        }
    }
}
